﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArbolandiaBL;

public partial class Site : System.Web.UI.MasterPage
{
    public Int32 totalItems = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (!Page.IsPostBack)
            {
                var service = new ServiceUI();
                var categories = service.GetCategorys();

                for (int i = 0; i < categories.Count(); i++)
                {
                    drpCategory.Items.Add(new ListItem(categories[i].Name, categories[i].Id.ToString()));
                }
            }
            SetCategorySelected();

            var items = new List<String>();
            if (HttpContext.Current.Session["arbolandiacart"] != null)
            {
                items = ((List<String>)HttpContext.Current.Session["arbolandiacart"]);
                //aki va el span
            }

            if (HttpContext.Current.Session["arbolandiacart"] != null)
            {
                totalItems = ((List<String>)HttpContext.Current.Session["arbolandiacart"]).Count();
            }

        }
        catch (Exception)
        {
            throw;
        }
    }
    protected void SetCategorySelected()
    {

        int categorySelected = 0;
        string category = Request.QueryString["c"];
        if (category != null && category != "")
        {
            Int32.TryParse(category, out categorySelected);
        }
        else
        {
            string subcategory = Request.QueryString["s"];
            int isub;
            Int32.TryParse(subcategory, out isub);
            if (isub > 0)
            {
                var cat = new ServiceUI().GetSubCategoryDetail(isub);
                if (cat != null)
                {
                    categorySelected = cat.IdCategory;
                }
            }

        }
        if (categorySelected > 0 && categorySelected <= 7)
        {
            HyperLink[] hplist = new HyperLink[8];
            hplist[1] = linkButton2;
            hplist[2] = linkButton1;
            hplist[3] = linkButton3;
            hplist[4] = linkButton4;
            hplist[5] = linkButton5;
            hplist[6] = linkButton6;
            hplist[7] = linkButton7;
            hplist[categorySelected].Style.Add("background-color", "#a8c32d"); hplist[categorySelected].Style.Add("text-decoration", "underline");
        }
    }

    /*
    protected void drpCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Categories.aspx?c=" + ((DropDownList)sender).SelectedValue + "&n=" + ((DropDownList)sender).SelectedItem);
        }
        catch (Exception)
        {            
            throw;
        }
    }
     * */
    protected void tbSearh_Click(object sender, EventArgs e)
    {
        try
        {
            string url = "";
            url = "sc=" + txtText.Text;
            if (drpCategory.SelectedValue != null)
            {
                url += "&c=" + drpCategory.SelectedValue;
            }
            Response.Redirect("AdvancedSearch.aspx?" + url);
        }
        catch (Exception) { }

    }
}
